""" 
this script convert origintime to utctime
convert_catalog.py 
"""
from distutils.log import error
import pandas as pd
import argparse
from obspy.core.utcdatetime import UTCDateTime
import os
from constant import CATALOG_PATH, OK027_POS, OK029_POS


def convert(args):
    try:
        os.path.isfile(args.catalog_path)
    except Exception as e:
        print("not a file")
    catalog = pd.read_csv(args.catalog_path)
    if 'utc_timestamp_new' not in catalog.columns:
        utc_timestamp = [UTCDateTime(t).timestamp for t in catalog['origintime']]
        catalog['utc_timestamp_new'] = utc_timestamp
    if 'utc_time' not in catalog.columns:
        utc_time = [UTCDateTime(t).datetime.isoformat(sep=' ', timespec='minutes') for t in catalog['origintime']]
        catalog['utc_time'] = utc_time
    else:
        print("have converted.")
        return
    print("Finished!")
    catalog.to_csv(args.catalog_path, index=False)


if __name__ == "__main__":
    print("convert_catalog")
    parser = argparse.ArgumentParser()
    parser.add_argument("--catalog_path", "-p", type=str, default=CATALOG_PATH,
                        help="the path of earthquake catalog")
    args = parser.parse_args()

    convert(args)
